﻿using ClientAppraise.Controls;
using ClientAppraise.Views.Pages.Corpus.CorpusAdornType.Admin;
using ClientDao.Corpus;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ShareCode.Dto;
using ShareCode.Entity.Corpus;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Wpf.Ui.Controls;

namespace ClientAppraise.ViewModels.Pages.Corpus.CorpusAdornType.Admin
{
    public partial class IndexViewModel(CorpusAdornTypeDal typeDal) : ObservableObject
    {
        CorpusAdornTypeDal Dal = typeDal;

        [ObservableProperty]
        private BaseTreeDataGridM<CorpusAdornTypeEntity>? selectedItems;
        partial void OnSelectedItemsChanged(BaseTreeDataGridM<CorpusAdornTypeEntity>? value)
        {
            IsEnabled = value != null;
        }

        [ObservableProperty]
        private bool isEnabled = false;

        /*
        [ObservableProperty]
        private bool? isChecked = false;
        partial void OnIsCheckedChanged(bool? value)
        {
            if (!value.HasValue) return;

            foreach (var item in ViewData)
            {
                item.IsChecked = value;
            }
        }
        */

        [ObservableProperty]
        private ObservableCollection<BaseTreeDataGridM<CorpusAdornTypeEntity>>? viewData;

        [RelayCommand]
        public void Add()
        {
            App.MyNavigation.NavigateWithHierarchy(typeof(NewPage));
        }

        [RelayCommand]
        public void AddSon()
        {
            App.MyNavigation.NavigateWithHierarchy(typeof(NewPage), new ClientInfrastructure.Dto.PageParameterDto() { Model = SelectedItems!.Entity, Do = ClientInfrastructure.Dto.PageParameterDtoDo.AddSon });
        }

        [RelayCommand]
        public void Edit()
        {
            App.MyNavigation.NavigateWithHierarchy(typeof(NewPage), new ClientInfrastructure.Dto.PageParameterDto() { Model = SelectedItems!.Entity, Do = ClientInfrastructure.Dto.PageParameterDtoDo.Update });
        }

        [RelayCommand]
        public async Task DeleteAsync()
        {
            if (SelectedItems != null && await new ZxMessageBox().ConfirmAsync("确定要删除选中项？") == Wpf.Ui.Controls.MessageBoxResult.Primary)
            {
                var reslut = await App.MyContentDialog.ShowRingLoadingAsync(async () =>
                {
                    try
                    {
                        ResultDto<bool> result;

                        if (SelectedItems!.Entity!.IsDelete == 1)
                        {
                            result = await Dal.UnLogicDeleteAsync(SelectedItems.Entity.Id);
                        }
                        else
                        {
                            result = await Dal.LogicDeleteAsync(SelectedItems.Entity.Id);

                        }
                        if (result != null && result.Success && result.Data)
                        {
                            App.MySnackbar.Success("成功操作");
                        }
                        else
                        {
                            App.MySnackbar.Danger(result!.Message??"");
                        }
                    }
                    catch (Exception ex)
                    {
                        App.MySnackbar.Danger(ex.Message, ex.Message);
                    }
                    finally
                    {
                        App.MyContentDialog.CloseLoading();
                    }
                });

                if (reslut == ContentDialogResult.Primary)
                {
                    await RefreshData();
                }

            }
        }

        public async Task RefreshData()
        {
            void fun(List<CorpusAdornTypeEntity>? viewData)
            {
                if (viewData == null) return;

                if (!Application.Current.Dispatcher.CheckAccess())
                {
                    Application.Current.Dispatcher.Invoke(new Action(() => fun(viewData)));
                    return;
                }
                var _viewData = BaseTreeDataGridM<CorpusAdornTypeEntity>.GetList(viewData);
                this.ViewData = _viewData;
            }

            await App.MyContentDialog.ShowRingLoadingAsync(async () =>
            {
                try
                {
                    var data = await Dal.ListAsync<CorpusAdornTypeEntity>(new PagingDto { PageSize = 999999 });

                    if (data.Success)
                    {
                        fun(data.Data);
                    }
                }
                catch (Exception ex)
                {
                    App.MySnackbar.Danger(ex.Message, ex.Message);
                }
                finally
                {
                    App.MyContentDialog.CloseLoading();
                }
            });
        }

    }
}
